package org.cskj.demo.controller;

import org.cskj.demo.pojo.Inshop;
import org.cskj.demo.pojo.Shop;
import org.cskj.demo.service.imp.InshopService;
import org.cskj.demo.service.imp.ShopImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/inshop")
public class InshopController {
    private final double size=3;
    @Autowired
    private InshopService inshopService;
    @Autowired
    private ShopImp shopImp;
    @RequestMapping("/all")
    private List<Inshop> all(int page){
        int count =inshopService.count();
        double pageMax=Math.ceil(count/size);
        System.out.println(count);
        System.out.println(pageMax);
        //计算page是否有效
        page=page>(int)(pageMax)?(int)(pageMax):page;
        //获取数
        List<Inshop> list = inshopService.selectAll(page, (int)size);
        return list;
    }
    @RequestMapping("/count")
    public int count(){
        int count = inshopService.count();
        return count;
    }
    @RequestMapping("/insert")
    public String insert(Inshop inshop,@RequestParam("uid") String uid){



        Integer u = Integer.valueOf(uid);
        inshop.setInaddtime(new Date());
        inshop.setUsid(u);
        int insert = inshopService.insert(inshop);

        Integer shopid = inshop.getShopid();
        Integer innum = inshop.getInnum();
        Shop shop = shopImp.selectByKey(shopid);
        shop.setShopnum(shop.getShopnum()+innum);
        int update = shopImp.update(shop);
        if (update>0){
            return "ok";
        }else {
            return "error";
        }
    }
}
